﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using enqueteJwVd.Models;

namespace enqueteJwVd.DAL
{
    public class UnitOfWork : IDisposable
    {
        #region Members

        #region Repositories
        private GenericRepository<QuestionGroup> questionGroupRepository;
        private GenericRepository<QuestionType> questionTypeRepository;
        private GenericRepository<Role> roleRepository;
        private GenericRepository<Survey> surveyRepository;
        private GenericRepository<SurveyQuestion> surveyQuestionRepository;
        private GenericRepository<User> userRepository;
        private GenericRepository<UserSurvey> userSurveyRepository;
        private GenericRepository<Answer> answerRepository;
        private GenericRepository<CheckBoxOptions> checkBoxOptionRepository;
        private GenericRepository<SurveyQuestionGroup> surveyQuestionGroupRepository;
        private GenericRepository<SurveyQuestionQuestionGroup> surveyQuestionQuestionGroupRepository;
        #endregion

        public IDbContext UoWContext { get; private set; }
        private bool disposed = false;

        #endregion

        #region Properties
        public GenericRepository<Answer> AnswerRepository
        {
            get
            {
                if (this.answerRepository == null)
                {
                    this.answerRepository =
                        new GenericRepository<Answer>(UoWContext);
                }
                return answerRepository;
            }
        }

        public GenericRepository<SurveyQuestionQuestionGroup> SurveyQuestionQuestionGroupRepository
        {
            get
            {
                if (this.surveyQuestionQuestionGroupRepository == null)
                {
                    this.surveyQuestionQuestionGroupRepository =
                        new GenericRepository<SurveyQuestionQuestionGroup>(UoWContext);
                }
                return surveyQuestionQuestionGroupRepository;
            }
        }

        public GenericRepository<SurveyQuestionGroup> SurveyQuestionGroupRepository
        {
            get
            {
                if (this.surveyQuestionGroupRepository == null)
                {
                    this.surveyQuestionGroupRepository =
                        new GenericRepository<SurveyQuestionGroup>(UoWContext);
                }
                return surveyQuestionGroupRepository;
            }
        }

        public GenericRepository<CheckBoxOptions> CheckBoxOptionRepository
        {
            get
            {
                if (this.checkBoxOptionRepository == null)
                {
                    this.checkBoxOptionRepository =
                        new GenericRepository<CheckBoxOptions>(UoWContext);
                }
                return checkBoxOptionRepository;
            }
        }
        
        public GenericRepository<QuestionGroup> QuestionGroupRepository
        {
            get
            {
                if (this.questionGroupRepository == null)
                {
                    this.questionGroupRepository =
                        new GenericRepository<QuestionGroup>(UoWContext);
                }
                return questionGroupRepository;
            }
        }

        public GenericRepository<QuestionType> QuestionTypeRepository
        {
            get
            {
                if (this.questionTypeRepository == null)
                {
                    this.questionTypeRepository = new GenericRepository<QuestionType>(UoWContext);
                }
                return questionTypeRepository;
            }
        }

        public GenericRepository<Role> RoleRepository
        {
            get
            {
                if (this.roleRepository == null)
                {
                    this.roleRepository = new GenericRepository<Role>(UoWContext);
                }
                return roleRepository;
            }
        }

        public GenericRepository<Survey> SurveyRepository
        {
            get
            {
                if (this.surveyRepository == null)
                {
                    this.surveyRepository = new GenericRepository<Survey>(UoWContext);
                }
                return surveyRepository;
            }
        }

        public GenericRepository<SurveyQuestion> SurveyQuestionRepository
        {
            get
            {
                if (this.surveyQuestionRepository == null)
                {
                    this.surveyQuestionRepository = new GenericRepository<SurveyQuestion>(UoWContext);
                }
                return surveyQuestionRepository;
            }
        }

        public GenericRepository<User> UserRepository
        {
            get
            {
                if (this.userRepository == null)
                {
                    this.userRepository = new GenericRepository<User>(UoWContext);
                }
                return userRepository;
            }
        }

        public GenericRepository<UserSurvey> UserSurveyRepository
        {
            get
            {
                if (this.userSurveyRepository == null)
                {
                    this.userSurveyRepository = new GenericRepository<UserSurvey>(UoWContext);
                }
                return userSurveyRepository;
            }
        }
        #endregion

        public UnitOfWork()
        {
            UoWContext = new VDLDbEntities();
        }

        public void SaveChanges()
        {
            UoWContext.SaveChanges();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    UoWContext.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}